﻿using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using AntC.AspNetCore.ApiEncryption.Internal;
using AntC.AspNetCore.ApiEncryption.Internal.Interfaces;
using Microsoft.Extensions.Options;

namespace AntC.AspNetCore.ApiEncryption.EncryptProviders
{
    /// <summary>
    /// Des加密方式提供器
    /// </summary>
    public class DesEncryptProvider : IEncryptProvider
    {
        /// <summary>
        /// 内容编码
        /// </summary>
        public Encoding ContentEncoding { get; set; }

        /// <summary>
        /// 加密密码 默认 1234567812345678
        /// </summary>
        public string Key { get; set; } = "1234567812345678";

        public DesEncryptProvider(IOptionsSnapshot<EncryptProviderOptions> options)
        {
            ContentEncoding = Encoding.GetEncoding(string.IsNullOrWhiteSpace(options.Value.EncodingName) ? "utf-8" : options.Value.EncodingName);
        }

        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="context"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public async Task<string> Decrypt(ApiEncryptionContext context, string content)
        {
            return Decrypt(content, Key);
        }

        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="context"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public async Task<string> Encrypt(ApiEncryptionContext context, string content)
        {
            context.HttpContext.Response.ContentType = $"text/plain; charset={ContentEncoding.HeaderName}";
            return Encrypt(content, Key);
        }

        /// <summary> 
        /// 加密数据 
        /// </summary> 
        /// <param name="text">需要加密的内容</param> 
        /// <param name="key">秘钥</param> 
        /// <returns></returns> 
        public string Encrypt(string text, string key)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            byte[] inputByteArray;
            inputByteArray = ContentEncoding.GetBytes(text);
            des.Key = ASCIIEncoding.ASCII.GetBytes(key.Substring(0, 8));
            des.IV = ASCIIEncoding.ASCII.GetBytes(key.Substring(8, 8));
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            return Convert.ToBase64String(ms.ToArray());
        }

        /// <summary> 
        /// 解密数据 
        /// </summary> 
        /// <param name="text">需要解密的内容</param> 
        /// <param name="key">秘钥</param> 
        /// <returns></returns> 
        public string Decrypt(string text, string key)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            byte[] inputByteArray = Convert.FromBase64String(text);

            des.Key = ASCIIEncoding.ASCII.GetBytes(key.Substring(0, 8));
            des.IV = ASCIIEncoding.ASCII.GetBytes(key.Substring(8, 8));
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            return ContentEncoding.GetString(ms.ToArray());
        }
    }
}
